﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace WSharp_Editor
{
    public partial class MainForm : Form
    {
        OpenFileDialog OpenWSFile = new OpenFileDialog();
        string[] wsfile;
        public static string t_wordWrap;
        public static string t_zoomFactor;
        public static string t_fontname;
        public static string t_fontsize;
        public static string t_fontstyle;
        public MainForm()
        {
            InitializeComponent();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenWSFile.Filter = "*.ws (WSharp)|*.ws";
            if (OpenWSFile.ShowDialog() == DialogResult.OK)
            {
                wsfile = File.ReadAllLines(OpenWSFile.FileName);
                foreach (string line in wsfile)
                {
                    editorTextBox.Text += line + Environment.NewLine;
                }
            }
        }

        private void textEditorSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextEditorSettings tmpTextEditorSettings = new TextEditorSettings(editorTextBox);
            if (tmpTextEditorSettings.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(t_wordWrap + Environment.NewLine +
                                t_zoomFactor + Environment.NewLine +
                                t_fontname + Environment.NewLine +
                                t_fontsize);
                editorTextBox.WordWrap = Boolean.Parse(t_wordWrap);
                editorTextBox.ZoomFactor = int.Parse(t_zoomFactor);
                FontStyle tmpFontStyle = FontStyle.Regular;
                switch(t_fontstyle){
                    case "Regular" :
                        tmpFontStyle = FontStyle.Regular;
                        break;
                    case "Italic" :
                        tmpFontStyle = FontStyle.Italic;
                        break;
                    case "Bold" :
                        tmpFontStyle = FontStyle.Bold;
                        break;
                    case "Strikeout" :
                        tmpFontStyle = FontStyle.Strikeout;
                        break;
                    case "Underline" :
                        tmpFontStyle = FontStyle.Underline;
                        break;
                }
                editorTextBox.Font = new Font(t_fontname, float.Parse(t_fontsize), tmpFontStyle);
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox tmpAboutBox = new AboutBox();
            tmpAboutBox.ShowDialog();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            wsfile = File.ReadAllLines(Program.arguments[0]);
            foreach (string line in wsfile)
            {
                editorTextBox.Text += line + Environment.NewLine;
            }
            string functiondatabasefile = Application.StartupPath + "/functiondatabase.db";
            string[] funcfile = File.ReadAllLines(functiondatabasefile);
            foreach (string line in funcfile)
            {
                try
                {
                    string[] funcitem = Regex.Split(line, "!");
                    functionlb.Items.Add(funcitem[0]);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            string variabledatabasefile = Application.StartupPath + "/variabledatabase.db";
            string[] varfile = File.ReadAllLines(variabledatabasefile);
            foreach (string line in varfile)
            {
                try
                {
                    string[] varitem = Regex.Split(line, "!");
                    variablelb.Items.Add(varitem[0]);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
        private void functionlb_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string functiondatabasefile = Application.StartupPath + "/functiondatabase.db";
            string[] funcarray = File.ReadAllLines(functiondatabasefile);
            editorTextBox.Text += Regex.Split(funcarray[functionlb.SelectedIndex], "!")[2];
        }

        private void variablelb_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string variabledatabasefile = Application.StartupPath + "/variabledatabase.db";
            string[] varfile = File.ReadAllLines(variabledatabasefile);
            editorTextBox.Text += Regex.Split(varfile[variablelb.SelectedIndex], "!")[2];
        }
    }
}
